package com.sicnu.estatemanagement.controller;

import com.github.pagehelper.Page;
import com.sicnu.estatemanagement.common.MessageConstant;
import com.sicnu.estatemanagement.common.PageResult;
import com.sicnu.estatemanagement.common.Result;
import com.sicnu.estatemanagement.common.StatusCode;
import com.sicnu.estatemanagement.domain.EstateManager;
import com.sicnu.estatemanagement.domain.LoginVo;
import com.sicnu.estatemanagement.service.EstateManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Auth: zhuan
 * @Desc: 小区管理控制层:只负责接收前端浏览器发送的请求和请求参数，调用service层获取业务逻辑加工处理后的数据
 */
@RestController
@CrossOrigin
@RequestMapping("/estate/manager")
public class EstateManagerController {
    
    @Autowired
    private EstateManagerService estateManagerService;

    @RequestMapping("/find")
    public Result find(){
        List<EstateManager> all = estateManagerService.findAll();
        return new Result(false,200,"请求成功",all);
    }
    @PostMapping("/login")
    public Result login(@RequestBody LoginVo loginVo, HttpSession httpSession){
        EstateManager manager = estateManagerService.login(loginVo);
        httpSession.setAttribute("user",manager);
        return new Result(true,200,"登录成功",manager);
    }
    @RequestMapping("/search")
    public PageResult search(@RequestBody Map searchMap){
        Page<EstateManager> page = estateManagerService.search(searchMap);
        return new PageResult(true, StatusCode.OK, MessageConstant.COMMUNITY_SEARCH_SUCCESS,page.getResult(),page.getTotal());
    }

    @RequestMapping("/add")
    public Result add(@RequestBody EstateManager estateManager){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd HH:mm:ss");
        LocalDateTime now = LocalDateTime.now();
        estateManager.setCreateTime(new Date(dtf.format(now)));
        Boolean add = estateManagerService.add(estateManager);
        return new Result(add,StatusCode.OK,MessageConstant.COMMUNITY_ADD_SUCCESS);
    }
    @RequestMapping("/findById")
    public Result findById(Integer id){
        EstateManager estateManager = estateManagerService.findById(id);
        return new Result(true,StatusCode.OK,MessageConstant.COMMUNITY_FIND_BY_ID_SUCCESS,estateManager);
    }

    @RequestMapping("/update")
    public Result update(@RequestBody EstateManager estateManager){
        Boolean add = estateManagerService.update(estateManager);
        return new Result(true,StatusCode.OK,MessageConstant.COMMUNITY_UPDATE_SUCCESS);
    }
    // /community/updateStatus/0/1
    @RequestMapping("/updateStatus/{status}/{id}")
    public Result updateStatus(@PathVariable("status") String status,@PathVariable("id") Integer id){
        Boolean flag = estateManagerService.updateStatus(status,id);
        return new Result(true,StatusCode.OK,MessageConstant.COMMUNITY_UPDATE_STATUS_SUCCESS);
    }
    @RequestMapping("/del")
    public Result del(@RequestBody List<Integer> ids){
        Boolean flag = estateManagerService.del(ids);
        return new Result(true,StatusCode.OK,MessageConstant.COMMUNITY_DELETE_SUCCESS);
    }
}
